package com.liwenfeng;

import com.liwenfeng.domain.Account;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

public class JDBCTemplateTest {
    {
        System.out.println("代码块1");
    }
    {
        System.out.println("代码块2");
    }
    public static void main(String[] args) {
        JDBCTemplateTest jdbcTemplateTest = new JDBCTemplateTest();
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/eesy");
        dataSource.setUsername("root");
        dataSource.setPassword("31000025");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");

/*
        ArrayList<Account> accounts = new ArrayList<>();
        try(Connection conn = dataSource.getConnection()){
            PreparedStatement statement = conn.prepareStatement("select * from account");
            ResultSet rs = statement.executeQuery();
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                double money = rs.getDouble("money");
                accounts.add(new Account(id,name,money));
            }
            for (Account account : accounts) {
                System.out.println(account);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
*/
        JdbcTemplate template = new JdbcTemplate(dataSource);
        List<Account> accounts = template.query("select * from account", (rs, row) -> {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            double money = rs.getDouble("money");
            return new Account(id, name, money);
        });
        for (Account account : accounts) {
            System.out.println(account);
        }

        Method[] methods = Account.class.getDeclaredMethods();
        for (Method method : methods) {
            System.out.println(method.getName());

        }
//        Field[] fields = Account.class.getDeclaredFields();
//        Constructor<?>[] constructors = Account.class.getConstructors();
//        Class<?>[] classes = Account.class.getDeclaredClasses();
    }
}
